﻿using System;
using Glue;
using GlueTests.Builders;
using Xunit;

namespace GlueTests.MappingTests
{
    public class When_adding_a_nested_relation
    {
        public class GuiPerson
        {
            public String Name { get; set; }
            public GuiAddress Address { get; set; }
        }

        public class GuiAddress
        {
            public String City { get; set; }
        }

        public class DomainPerson
        {
            public String Name { get; set; }
            public DomainAddress Address { get; set; }
        }

        public class DomainAddress
        {
            public String City { get; set; }
        }

        private readonly Mapping<DomainPerson, GuiPerson> personMapping;

        public When_adding_a_nested_relation()
        {
            var addressMap = new Mapping<DomainAddress, GuiAddress>();
            personMapping = new Mapping<DomainPerson, GuiPerson>();
            personMapping.AddConverter(addressMap);
        }

        [Fact]
        public void Should_accept_nested_relation()
        {
            personMapping.Relate(a => a.Address, b => b.Address);
            var relations = personMapping.Relations;
            Assert.True(relations.IsRelatedTowardsOnLeftSide(new PropertyBuilder(typeof(DomainPerson),"Address").Build())); 
        }

        [Fact]
        public void Should_relate_on_autorelate()
        {
            personMapping.AutoRelateEqualNames();
            var relations = personMapping.Relations;
            Assert.True(relations.IsRelatedTowardsOnLeftSide(new PropertyBuilder(typeof(DomainPerson), "Address").Build())); 
        }
    }
}